package com.hnichr.ump.build7.message.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hnichr.ump.build7.message.MessageArrivedHandle;
import com.hnichr.ump.build7.pojo.Plate;
import com.hnichr.ump.build7.share.WhitelistQueryShare;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class WhiteListQueryMessageArrivedHandle implements MessageArrivedHandle {
    private static final Logger logger = LoggerFactory.getLogger(WhiteListQueryMessageArrivedHandle.class);

    @Override
    public boolean support(String topic, String message) {
        if(!"b956ba98-988cd83e/device/message/down/white_list_operator/reply".equals(topic)){
            return false;
        }
        JSONObject jsonObject = JSON.parseObject(message);
        return jsonObject.containsKey("payload")&&jsonObject.get("payload")!=null;
    }

    @Override
    public void handle(String topic, String message) {
        try{
            JSONObject jsonObject = JSON.parseObject(message);
            JSONObject payload = (JSONObject)jsonObject.get("payload");
            JSONObject body = (JSONObject)payload.get("body");
            JSONArray rec = (JSONArray)body.get("dldb_rec");
            List<Plate> plateList = new ArrayList<>();
            if(rec!=null){
                for (Object o : rec) {
                    JSONObject plateObj = (JSONObject) o;
                    String plate = plateObj.getString("plate");
                    String enableTime = plateObj.getString("enable_time");
                    String overdueTime = plateObj.getString("overdue_time");
                    int enable = plateObj.getInteger("enable");
                    String context = plateObj.getString("context");
                    int timeSegEnable = plateObj.getInteger("time_seg_enable");
                    String segTimeStart = plateObj.getString("seg_time_start");
                    String segTimeEnd = plateObj.getString("seg_time_end");
                    int needAlarm = plateObj.getInteger("need_alarm");

                    Plate plateVo = new Plate();
                    plateVo.setPlate(plate);
                    plateVo.setEnableTime(enableTime);
                    plateVo.setOverdueTime(overdueTime);
                    plateVo.setEnable(enable);
                    plateVo.setContext(context);
                    plateVo.setTimeSegEnable(timeSegEnable);
                    plateVo.setSegTimeStart(segTimeStart);
                    plateVo.setSegTimeEnd(segTimeEnd);
                    plateVo.setNeedAlarm(needAlarm);
                    plateList.add(plateVo);
                }
            }
            synchronized (WhitelistQueryShare.getInstance()){
                WhitelistQueryShare.getInstance().setPlateList(plateList);
                WhitelistQueryShare.getInstance().notifyAll();
            }
        }
        catch (Exception e){
            logger.error("WhiteListQueryMessageArrivedHandle报错",e);
        }


    }
}
